const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 12,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.shopId) {
      this.setData({
        shopId: options.shopId
      })
    }
    if (options.index == 1) {
      this.setData({
        isShop: true,
        index: options.index
      })
    } else if (options.index == 2) {
      this.setData({
        isShop: false,
        index: options.index
      })
    }

    if (options.q) {
      var inn_id = options.q;
      var inn_url = unescape(decodeURI(inn_id));

      var str = inn_url.split("?")[1],
        items = str.split("&");
      var arr, name, value, obj = {};
      for (var i = 0, l = items.length; i < l; i++) {
        arr = items[i].split("=");
        name = arr[0];
        value = arr[1];
        obj[name] = value;
      }
      // console.log(obj);
      this.setData(obj)
      if (obj.index == 1) {
        this.setData({
          isShop: true,
          index: obj.index
        })
      } else if (obj.index == 2) {
        this.setData({
          isShop: false,
          index: obj.index
        })
      }
    }



    this.goodsList();

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      url: app.globalData.base_url + 'images/shore_blank.png'
    })
  },

  goodsList: function () {
    var params = new Object();
    var that = this;
    params.limit = this.data.limit;
    params.page = this.data.page;
    if (this.data.shopId) {
      params.shop_id = this.data.shopId;
    }
    console.log(params);
    api.POST({
      params: params,
      url: app.globalData.base_url + 'shop/getGoodsList',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.search;
          var totalPage = paging.total_page;
          var resultList = res.data.data.goods_list;
          var shopName = res.data.data.shop_info.shop_name;
          var shopAddress = res.data.data.shop_info.address;
          var shopAvatar = res.data.data.shop_info.avatar;
          var shop_info = res.data.data.shop_info;
           var business_hours = res.data.data.shop_info.business_hours;
          if (that.data.page == 1) {
            var itemData = resultList;
          } else {
            var itemData = that.data.product.concat(resultList)
          }
          that.setData({
            total_page: totalPage,
            product: itemData,
            shop_info:shop_info,
            shopName: shopName,
            shopAddress: shopAddress,
            shopAvatar: shopAvatar,
             business_hours: business_hours
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("网络错误，请检查网络");

      }
    })
  },
  //刷新页面
  resetData: function () {
    var that = this
    that.setData({
      page: 1,
      hasMore: true
    });
  },
  //页面下拉刷新
  onPullDownRefresh: function () {
    var that = this;

    setTimeout(function () {
      that.resetData()
    }, 1000)
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },
  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.goodsList();
  },

  //查看商品详情
  productDetails: function (e) {
    var sku_code = e.currentTarget.dataset.sku_code;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    if (this.data.isShop == false) {
      wx.navigateTo({
        url: '/index/pages/productDetails/productDetails?sku_code=' + sku_code + '&shop_id=' + this.data.shopId
      })
    }
  },

  // 导航
  navigation: function (e) {
    var latitude = this.data.shop_info.add_info.location_latitude / 1;
    var longitude = this.data.shop_info.add_info.location_longitude / 1;
    wx.openLocation({
      latitude,
      longitude,
      scale: 16
    })
  },
  navigationIndex:function(e){
    wx.switchTab({
      url: '/pages/index/index'
    })
  }

})